import axios from 'axios'
const base64Prefix = 'data:image/png;base64,'

function EloamInterface () {

    /**
     * 关闭主摄像头
     * @param callback
     * @param object
     */
    this.close_view1 = function (callback,object) {
        let params = {"camidx": "0"}
        axios.post("http://127.0.0.1:38088/video=close", JSON.stringify(params)).then((response)=>{
            var data = response.data || {}
            var code = data.code
            var succ = code === '0'
            // 请求成功处理
            console.log(response.data)
            callback.call(object, succ)
        }).catch(error => {
            // 请求失败处理
            console.error(error)
            callback.call(object, false)
        })
    }

    /**
     * 关闭副摄像头
     * @param callback
     * @param object
     */
    this.close_view2 = function (callback,object) {
        let params = {"camidx": "1"}
        axios.post("http://127.0.0.1:38088/video=close", JSON.stringify(params)).then((response)=>{
            var data = response.data || {}
            var code = data.code
            var succ = code === '0'
            // 请求成功处理
            console.log(response.data)
            callback.call(object, succ)
        }).catch(error => {
            // 请求失败处理
            console.error(error)
            callback.call(object, false)
        })
    }

    /**
     * 主摄像头拍照
     */
    this.view1_scan = function (config,callback,object) {
        let params = {
            "filepath": config.filepath,
            "rotate": "0",
            "cutpage": "0",
            "camidx": "0",
            "ColorMode": "0",
            "quality": "3"
        }
        axios.post("http://127.0.0.1:38088/video=grabimage", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.filepath
            if(!succ){
                data = ''
            }
            callback.call(object, succ, data)
            console.log('主头拍照成功',res)
        }).catch(function (error) {
            console.log('主摄像头拍照失败',error)
            callback.call(object, false)
        })
    }

    /**
     * 副摄像头拍照
     */
    this.view2_scan = function (callback,object) {
        let params = {
            "filepath": "base64",
            "rotate": "0",
            "cutpage": "0",
            "camidx": "1",
            "ColorMode": "0",
            "quality": "3"
        }
        axios.post("http://127.0.0.1:38088/video=grabimage", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.photoBase64
            if(!succ){
                data = ''
            }
            callback.call(object, succ, data)
            console.log("副头拍照成功")
        }).catch(function (error) {
            console.log('副摄像头拍照失败',error)
            callback.call(object, false)
        })
    }

    /**
     * 主摄像头拍照
     */
    this.view_scan = function (params,callback,object) {
        axios.post("http://127.0.0.1:38088/video=grabimage", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.filepath
            if(!data){
                data = res.data.photoBase64
            }
            if(!succ){
                data = ''
            }
            callback.call(object, succ, data)
            console.log('主头拍照成功',res)
        }).catch(function (error) {
            console.log('主摄像头拍照失败',error)
            callback.call(object, false)
        })
    }
    /**
     * 设置预览放大缩小
     * @param val
     */
    this.view1_zoominout = function (val, callback, object) {
        let params = {
            "camidx": "0",
            "corp": val
        }
        axios.post("http://127.0.0.1:38088/stream=zoominout", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            callback.call(object, succ)
            console.log("设置主摄像头分辨率成功",res)
        }).catch(function (error) {
            console.log('设置主摄像头分辨率失败',error)
            callback.call(object, false)
        })
    }
    /**
     * 二寸照（需要开启副头，设备需要带人脸芯片）
     */
    this.twoinchphoto = function (callback, object) {
        let params = {}
        axios.post("http://127.0.0.1:38088/twoinchphoto", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.data
            if(!succ){
                data = ''
            }else{
                data = 'data:image/png;base64,' + data
            }
            callback.call(object, succ, data)
            console.log("二寸照获取成功",res)
        }).catch(function (error) {
            console.log('二寸照获取失败',error)
            callback.call(object, false)
        })
    }
    /**
     * 读取身份证号
     */
    this.readIdcard = function (callback, object) {
        axios.get("http://127.0.0.1:38088/card=idcard").then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.data
            if(!succ){
                data = ''
            }else{
                data = 'data:image/png;base64,' + data
            }
            callback.call(object, succ, data)
            console.log("读取身份证号成功",res)
        }).catch(function (error) {
            console.log('读取身份证号失败',error)
            callback.call(object, false)
        })
    }
    /**
     * 读取IC卡信息
     */
    this.readIccard = function (callback, object) {
        let params = {
            iccardtype: '1'
        }
        axios.post("http://127.0.0.1:38088/card=iccard", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.data
            if(!succ){
                data = ''
            }else{
                data = 'data:image/png;base64,' + data
            }
            console.log("读取IC卡信息成功",res)
            callback.call(object, succ, data)
        }).catch(function (error) {
            console.log('读取IC卡信息失败',error)
            callback.call(object, false)
        })
    }
    /**
     * ocr
     */
    this.ocr = function (params, callback, object) {
        console.log('ocr', params)
        axios.post("http://127.0.0.1:38088/video=ocr", JSON.stringify(params)).then((res)=>{
            console.log('ocr', res)
            var succ = res.data.code === '0'
            var data = res.data.UrlEncodeData
            if(!succ){
                callback.call(object, succ, JSON.stringify(res))
                return
            }
            data = decodeURIComponent(data)
            var base64 = res.data.filebase64
            console.log("ocr成功",res)
            callback.call(object, succ, data, base64)
        }).catch(function (error) {
            console.log('ocr失败',error)
            callback.call(object, false, JSON.stringify(error))
        })
    }
    /**
     * 获取设备状态
     * @param callback
     * @param object
     */
    this.videoStatus = function (callback, object) {
        let params = {

        }
        axios.post("http://127.0.0.1:38088/video=status", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var video0 = 'no', video1 = 'no'
            if(succ){
                video0 = res.data.video0
                video1 = res.data.video1
            }
            console.log("获取设备状态成功",res)
            callback.call(object, succ, video0, video2)
        }).catch(function (error) {
            console.log('获取设备状态失败',error)
            callback.call(object, false)
        })
    }
    /**
     * 判断设备是否已连接
     * @param callback
     * @param object
     */
    this.checkConnect = function (callback, object) {
        let params = {

        }
        axios.post("http://127.0.0.1:38088/device=isconnect", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.data
            var cnt = 0
            if(succ){
                cnt = parseInt(data)
            }
            console.log("判断设备是否已连接成功",res)
            callback.call(object, succ, cnt)
        }).catch(function (error) {
            console.log('判断设备是否已连接失败',error)
            callback.call(object, false)
        })
    }
    /**
     *  展平拍照
     * @param callback
     * @param object
     */
    this.videoAutoflat = function (callback, object) {
        let params = {
            filepath: '',
            rotate: '0',
            camidx: '0',
            cutpage: '0',
            autoflat: {
                flat: '1',
                leftfilepath: 'D://testleft.jpg',
                rightfilepath: 'D://testleft.jpg',
                removefinger: '1',
                doublepage: '1'
            }
        }
        axios.post("http://127.0.0.1:38088/video=autoflat", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var photoBase64 = base64Prefix + res.data.leftphotoBase64
            console.log(" 展平拍照成功",res)
            callback.call(object, succ, photoBase64)
        }).catch(function (error) {
            console.log(' 展平拍照失败',error)
            callback.call(object, false)
        })
    }
    /**
     * 获取主摄像头分辨率
     */
    this.view_getresolution = function (params, callback, object) {
        axios.post("http://127.0.0.1:38088/device=getresolution", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.data
            console.log("获取摄像头分辨率成功",res)
            callback.call(object, succ, data)
        }).catch(function (error) {
            console.log('获取摄像头分辨率失败',error)
            callback.call(object, false)
        })
    }
    this.view1_setresolution = function (callback, object) {
        let params = {
            // 必选，摄像头，0：主头，1：副头
            camidx: '1',
            // 必选，视频模式，0：YUY2（高清），1：MJPG（高速）
            mode: '0',
            //视频宽度
            width: '1280',
            //视频高度
            height: '1024'
        }
        axios.post("http://127.0.0.1:38088/device=setresolution", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            var data = res.data.data
            console.log("设置主摄像头分辨率成功",res)
            callback.call(object, succ, data)
        }).catch(function (error) {
            console.log('设置主摄像头分辨率失败',error)
            callback.call(object, false)
        })
    }
    /**
     * 开始录制视频
     * @param callback
     * @param object
     */
    this.view1_start = function (callback, object) {
        let params = {
            action: 'start',
            parameter: {
                camidx: '0',
                width: '640',// 视频宽度，默认：640
                height: '480',// 视频高度，默认：480
                framerate: '10',// 视频帧率，默认：10
                filepath: '',// 文件保存地址，为空：生成临时文件
                bit_rate: '400000'// 视频码率，默认：400000
            }
        }
        axios.post("http://127.0.0.1:38088/video=record", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            console.log("开始录制视频成功",res)
            var data = res.data.data
            callback.call(object, succ, data)
        }).catch(function (error) {
            console.log('开始录制视频失败',error)
            callback.call(object, false)
        })
    }
    /**
     * 重新启动主摄像头
     * @param callback
     * @param object
     */
    this.view1_restart = function (callback,  object) {
        this.close_view1(function () {
            this.view1_start(callback, object)
        },this)
    }

    /**
     * 开启/关闭自动拍照
     */
    this.movedetec = function (params, callback,  object) {
        console.log('movedetec', params)
        axios.post("http://127.0.0.1:38088/video=movedetec", JSON.stringify(params)).then((res)=>{
            var succ = true
            console.log("开启/关闭自动拍照",res)
            var data = res.data
            callback.call(object, succ, data)
        }).catch(function (error) {
            console.log('开启/关闭自动拍照失败',error)
            callback.call(object, false)
        })
    }

    /**
     * 纠偏裁边
     * @param params
     * @param callback
     * @param object
     */
    this.cameradeskew = function (params, callback, object) {
        axios.post("http://127.0.0.1:38088/dvideo=cameradeskew", JSON.stringify(params)).then((res)=>{
            var succ = res.data.code === '0'
            console.log("开启/关闭纠偏裁边",res)
            callback.call(object, succ)
        }).catch(function (error) {
            console.log('开启/关闭纠偏裁边失败',error)
            callback.call(object, false)
        })
    }

    /**
     * 选择摄像头
     * @param params
     * @param callback
     * @param object
     */
    this.rotation = function (params, callback, object) {
        axios.post("http://127.0.0.1:38088/video=rotate", JSON.stringify(params)).then((res)=>{
            console.log('旋转镜头', res)
            if (res.code === "0") {
                callback.call(object, true)
            } else {
                callback.call(object, false)
            }
        }).catch(function (error) {
            console.log('旋转镜头',error)
            callback.call(object, false)
        })
    }

}
export default new EloamInterface()
